package ${clazz.pkgName};

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * @author ${clazz.author}
 * @date ${clazz.date}
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = ${clazz.className}.ValueRangeValidator.class)
public @interface ${clazz.className} {
    String[] value();

    String message() default "数值不正确";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};


    class ValueRangeValidator implements ConstraintValidator<${clazz.className}, Object> {
        private String[] values;

        @Override
        public void initialize(ValueRange constraintAnnotation) {
            values = constraintAnnotation.value();
        }

        @Override
        public boolean isValid(Object value, ConstraintValidatorContext context) {
            if (value == null) {
                return true;
            }
            String s = value.toString();
            for (String value1 : values) {
                if (s.equals(value1)) {
                    return true;
                }
            }
            return false;
        }
    }
}
